package com.hmo.appshop.mamager.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hmo.appshop.mamager.bean.App;
import com.hmo.appshop.mamager.custom.ServiceException;
import com.hmo.appshop.mamager.dto.commom.MyFileUtils;
import com.hmo.appshop.mamager.mapper.AppMapper;
import lombok.extern.slf4j.Slf4j;
import net.dongliu.apk.parser.ApkFile;
import net.dongliu.apk.parser.bean.ApkMeta;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Slf4j
@Service("AppService")
public class AppService {

    @Autowired
    AppMapper appMapper;
    @Autowired
    MyFileUtils myFileUtils;

    public App apk(MultipartFile multipartFile){
        App app = new App();
        ApkFile apkFile = null;
        ApkMeta apkMeta;
        File file;
        try {

            file = myFileUtils.saveFile(3,multipartFile);

            apkFile = new ApkFile(file);
            apkMeta = apkFile.getApkMeta();

            String s = myFileUtils.openApkSaveIcon(apkMeta.getIcon(), file);
            app.setLogo(s);

            app.setName(apkMeta.getLabel());

            app.setVersion(apkMeta.getVersionName());

            app.setSize((double) (file.length() * 100 / 1024 / 1024) / 100 + " MB");

            app.setPostdate(System.currentTimeMillis());

            app.setType(0);

            app.setApk(file.getName());

        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }finally {
            try {
                apkFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        int insert = appMapper.insertOne(app);
        if (insert==0)
            throw new AuthenticationServiceException("服务器异常");
        return app;
    }

    public App update(App app){
        int i = appMapper.updateById(app);
        if (i==0)
            throw new AuthenticationServiceException("服务器异常");
        return selectOne(app.getId());
    }

    public App selectOne(int id){
        App app = appMapper.selectOne(new QueryWrapper<App>().eq("id", id));
        if (app==null)
            throw new ServiceException(500,"应用未找到");
        return app;
    }

    public App updateLogo(MultipartFile multipartFile,int id){
        App app = selectOne(id);
        myFileUtils.delFile(1,app.getLogo());

        File file = myFileUtils.saveFile(1, multipartFile);
        app.setLogo(file.getName());
        appMapper.updateById(app);
        return app;
    }

    public String addPicture(MultipartFile multipartFile,int id){
        App app = selectOne(id);

        File file = myFileUtils.saveFile(2, multipartFile);

        String pictures = app.getPictures();
        if (pictures==null ||pictures.length() <= 0)
            app.setPictures(file.getName());
        else
            app.setPictures(pictures+"|"+file.getName());

        appMapper.updateById(app);

        return app.getPictures();
    }

    public String delPicture(String name,int id){
        App app = selectOne(id);

        if (app.getPictures()==null)
            return null;
        String setP=null;
        String[] p = app.getPictures().split("\\|");
        for (String s:p){
            if (name.equals(s)) {
                myFileUtils.delFile(2, name);

            }else {
                if (setP==null){
                    setP=s;
                }
                else
                    setP=setP+"|"+s;
            }
        }
        if (app.getPictures().equals(setP))
            return null;
        else{
            app.setPictures(setP);
            appMapper.updateById(app);
        }
        return setP;

    }

    public List<App> selectApp(int type){
        List<App> appList = appMapper.selectList(new QueryWrapper<App>().eq("type", type));
        return appList;
    }

    public int delApp(int id){
        App app = selectOne(id);
        myFileUtils.delFile(1,app.getLogo());
        if (app.getPictures()!=null){
            String[] p = app.getPictures().split("\\|");
            for (String s:p){
                myFileUtils.delFile(2, s);
            }
        }
        myFileUtils.delFile(3,app.getApk());
        int i = appMapper.deleteById(id);
        return i;
    }
}
